<?php
namespace Home\Controller;

header('Content-Type:text/html;charset=utf-8');
class LoginController extends AuthController {
    public function index(){
        $this->display('index');
    }
	
	public function skip(){//登录验证
		if(IS_POST){
            $name = I('post.user_name');
            $pass = md5(I('post.password'));
            $vips = M("user");
            $vip = $vips->where("password='%s' AND (nickname='%s' OR email='%s' OR tel='%s' OR user_name='%s')",array($pass,$name,$name,$name,$name))->find();
            if($vip){
                if(M('user')->where('user_name = "'.$vip['user_name'].'"')->save(array('ltime'=>time()))){   //修改登录时间
                    session('home_user' ,$vip);
                    cookie('home_user' , $vip);
                    $this->success('登录成功',U('index/index'));
                }else{
                    $this->error('登录失败');
                }
            }else{
                $this->error('登录失败');
            }
		}
    }
	
	public function log_out(){//注销登录
		$_SESSION['home_user'] = null;
		echo " <div id=\"errorfrm\"> 
				<p>正在注销,请耐心等待 <span id=\"mes\">3</span> 秒钟！</p> 
				<script> 
				var i = 3; 
				var intervalid; 
				intervalid = setInterval(\"fun()\", 1000); 
				function fun() { 
				if (i == 0) { 
				clearInterval(intervalid); 
				location.href='".U('login/index')."';
				} 
				document.getElementById(\"mes\").innerHTML = i; 
				i--; 
				} 
				</script> ";
    }

    public function register(){
	    if(IS_POST){
	        //dump(I('post.'));
            $vips = D("user");
            if($vips->create()){
               // dump(I('post.'));exit;
                if($vips->add()){
                    $this->success('注册成功',U('login/index'));
                }else{
                    $this->error('注册失败');
                }
            }else{
                $this->error($vips->getError());
            }
        }
	    $this->display();
    }

    public function reg_ajax(){
        if(I('post.user_name') == ''){
            $this->ajaxReturn(0);
        }else if(strlen(I('post.user_name'))>=6){
            $user = M('user')->where('user_name = "'.I('post.user_name').'"')->find();
            if(empty($user)){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(2);
            }
        }else{
            $this->ajaxReturn(2);
        }
    }

	
}